<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册 - 智能代码评估系统</title>
    {% load static %}
    <link href="{% static 'vendor/bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet">
    <style>
        :root {
            --primary: #4285f4;
            --secondary: #34a853;
            --danger: #ea4335;
            --dark: #2d3748;
            --light: #f7fafc;
            --gray: #718096;
        }

        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            margin: 0;
            padding: 20px;
        }

        .register-card {
            width: 100%;
            max-width: 480px;
            background: white;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
            overflow: hidden;
            transition: transform 0.3s ease;
        }

        .register-card:hover {
            transform: translateY(-5px);
        }

        .register-header {
            padding: 30px 30px 20px;
            text-align: center;
            background: linear-gradient(to right, var(--primary), var(--secondary));
            color: white;
        }

        .register-header h2 {
            font-weight: 600;
            margin: 0;
            font-size: 1.8rem;
        }

        .register-header p {
            opacity: 0.9;
            margin: 8px 0 0;
            font-size: 0.95rem;
        }

        .register-body {
            padding: 25px 40px 30px;
        }

        .user-type-tabs {
            display: flex;
            margin-bottom: 25px;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
        }

        .user-type-tab {
            flex: 1;
            padding: 12px;
            text-align: center;
            background: #f8f9fa;
            cursor: pointer;
            transition: all 0.3s;
            border: none;
            font-weight: 500;
        }

        .user-type-tab:first-child {
            border-radius: 8px 0 0 8px;
        }

        .user-type-tab:last-child {
            border-radius: 0 8px 8px 0;
        }

        .user-type-tab.active {
            background: linear-gradient(to right, var(--primary), var(--secondary));
            color: white;
        }

        .form-group {
            margin-bottom: 20px;
            position: relative;
        }

        .form-label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: var(--dark);
        }

        .form-label.required:after {
            content: " *";
            color: var(--danger);
        }

        .form-control {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #e2e8f0;
            border-radius: 8px;
            font-size: 0.95rem;
            transition: all 0.3s;
        }

        .form-control:focus {
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.2);
        }

        .form-control.is-invalid {
            border-color: var(--danger);
        }

        .form-hint {
            font-size: 0.8rem;
            color: var(--gray);
            margin-top: 6px;
        }

        .terms-check {
            display: flex;
            align-items: center;
            margin: 25px 0;
        }

        .terms-check input {
            margin-right: 10px;
        }

        .terms-check label {
            font-size: 0.9rem;
            color: var(--gray);
        }

        .terms-check a {
            color: var(--primary);
            text-decoration: none;
        }

        .terms-check a:hover {
            text-decoration: underline;
        }

        .btn-register {
            width: 100%;
            padding: 14px;
            background: linear-gradient(to right, var(--primary), var(--secondary));
            color: white;
            border: none;
            border-radius: 8px;
            font-weight: 500;
            font-size: 1rem;
            cursor: pointer;
            transition: all 0.3s;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        .btn-register:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
        }

        .login-link {
            text-align: center;
            margin-top: 20px;
            font-size: 0.9rem;
            color: var(--gray);
        }

        .login-link a {
            color: var(--primary);
            text-decoration: none;
            font-weight: 500;
        }

        .login-link a:hover {
            text-decoration: underline;
        }

        .error-message {
            color: var(--danger);
            font-size: 0.9rem;
            margin-top: 5px;
            display: none;
        }

        @media (max-width: 576px) {
            .register-card {
                max-width: 100%;
            }

            .register-body {
                padding: 20px;
            }
        }
    </style>
</head>
<body>
    <div class="register-card">
        <div class="register-header">
            <h2><i class="fas fa-user-plus"></i> 创建账号</h2>
            <p>加入智能代码评估系统，开启高效开发体验</p>
        </div>

        <div class="register-body">
            <form id="registerForm" method="post" action="/register/">
                {% csrf_token %}

                {% if error_message %}
                <div class="alert alert-danger mb-4">{{ error_message }}</div>
                {% endif %}

                <div class="user-type-tabs">
                    <button type="button" class="user-type-tab active" data-type="employee">员工注册</button>
                    <button type="button" class="user-type-tab" data-type="admin">管理员注册</button>
                    <input type="hidden" name="user_type" id="userType" value="employee">
                </div>

                <div class="form-group">
                    <label for="realName" class="form-label required">真实姓名</label>
                    <input type="text" id="realName" class="form-control" name="real_name" placeholder="请输入真实姓名" required value="{{ real_name|default:'' }}">
                    <div class="error-message" id="realNameError">请输入真实姓名</div>
                </div>

                <div class="form-group">
                    <label for="username" class="form-label required">账号</label>
                    <input type="text" id="username" class="form-control" name="username" placeholder="4-16位字符" required value="{{ username|default:'' }}">
                    <div class="error-message" id="usernameError">用户名长度应为4-16位</div>
                </div>

                <div class="form-group">
                    <label for="email" class="form-label required">电子邮箱</label>
                    <input type="email" id="email" class="form-control" name="email" placeholder="请输入有效邮箱地址" required value="{{ email|default:'' }}">
                    <div class="error-message" id="emailError">请输入有效的邮箱地址</div>
                </div>

                <div class="form-group">
                    <label for="employeeId" class="form-label required">工号</label>
                    <input type="text" id="employeeId" class="form-control" name="employee_id" placeholder="请输入工号" required value="{{ employee_id|default:'' }}">
                    <div class="error-message" id="employeeIdError">请输入有效工号</div>
                </div>

                <div class="form-group">
                    <label for="position" class="form-label">职位</label>
                    <input type="text" id="position" class="form-control" name="position" placeholder="选填" value="{{ position|default:'' }}">
                </div>
                <div class="form-group">
                    <label for="password" class="form-label required">密码</label>
                    <input type="password" id="password" class="form-control" name="password" placeholder="至少6位字符" required>
                    <div class="form-hint">建议使用字母、数字和特殊符号的组合</div>
                    <div class="error-message" id="passwordError">密码长度至少6位</div>
                </div>

                <div class="form-group">
                    <label for="confirmPassword" class="form-label required">确认密码</label>
                    <input type="password" id="confirmPassword" class="form-control" name="confirm_password" placeholder="请再次输入密码" required>
                    <div class="error-message" id="confirmPasswordError">两次密码输入不一致</div>
                </div>

                <div class="terms-check">
                    <input type="checkbox" id="agreeTerms" name="agree_terms" required>
                    <label for="agreeTerms">我已阅读并同意<a href="#">服务条款</a>和<a href="#">隐私政策</a></label>
                    <div class="error-message" id="termsError">请同意服务条款和隐私政策</div>
                </div>

                <button type="submit" class="btn-register">立即注册</button>

                <div class="login-link">
                    已有账号? <a href="/login">立即登录</a>
                </div>
            </form>
        </div>
    </div>

    <script>
        // 用户类型切换
        document.querySelectorAll('.user-type-tab').forEach(tab => {
            tab.addEventListener('click', function() {
                document.querySelectorAll('.user-type-tab').forEach(t => t.classList.remove('active'));
                this.classList.add('active');
                document.getElementById('userType').value = this.dataset.type;
            });
        });

        // 表单验证
        const form = document.getElementById('registerForm');
        const inputs = {
            realName: document.getElementById('realName'),
            username: document.getElementById('username'),
            email: document.getElementById('email'),
            employeeId: document.getElementById('employeeId'),
            password: document.getElementById('password'),
            confirmPassword: document.getElementById('confirmPassword'),
            agreeTerms: document.getElementById('agreeTerms')
        };

        // 邮箱验证正则表达式
        const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

        // 实时验证
        inputs.username.addEventListener('input', function() {
            const valid = this.value.length >= 4 && this.value.length <= 16;
            toggleError(this, 'usernameError', !valid && this.value.length > 0);
        });

        inputs.email.addEventListener('input', function() {
            const valid = emailRegex.test(this.value);
            toggleError(this, 'emailError', !valid && this.value.length > 0);
        });

        inputs.password.addEventListener('input', function() {
            const valid = this.value.length >= 6;
            toggleError(this, 'passwordError', !valid && this.value.length > 0);
        });

        inputs.confirmPassword.addEventListener('input', function() {
            const valid = this.value === inputs.password.value;
            toggleError(this, 'confirmPasswordError', !valid && this.value.length > 0);
        });

        // 提交验证
        form.addEventListener('submit', function(e) {
            let isValid = true;

            // 必填字段验证
            if (!inputs.realName.value.trim()) {
                toggleError(inputs.realName, 'realNameError', true);
                isValid = false;
            }

            if (!inputs.email.value.trim() || !emailRegex.test(inputs.email.value)) {
                toggleError(inputs.email, 'emailError', true);
                isValid = false;
            }

            if (!inputs.employeeId.value.trim()) {
                toggleError(inputs.employeeId, 'employeeIdError', true);
                isValid = false;
            }

            if (!inputs.agreeTerms.checked) {
                document.getElementById('termsError').style.display = 'block';
                isValid = false;
            }

            // 密码一致性验证
            if (inputs.password.value !== inputs.confirmPassword.value) {
                toggleError(inputs.confirmPassword, 'confirmPasswordError', true);
                isValid = false;
            }

            if (!isValid) {
                e.preventDefault();
                // 滚动到第一个错误处
                const firstError = document.querySelector('.error-message[style="display: block;"]');
                if (firstError) {
                    firstError.scrollIntoView({ behavior: 'smooth', block: 'center' });
                }
            }
        });

        function toggleError(input, errorId, show) {
            const errorElement = document.getElementById(errorId);
            if (show) {
                input.classList.add('is-invalid');
                errorElement.style.display = 'block';
            } else {
                input.classList.remove('is-invalid');
                errorElement.style.display = 'none';
            }
        }
    </script>
</body>
</html>